package com.opencloud.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.Result;
import com.opencloud.common.security.http.XRestTemplate;
import com.opencloud.system.client.model.entity.SystemAction;
import com.opencloud.system.client.model.entity.SystemMenu;
import com.opencloud.system.client.service.ISystemMenuServiceClient;
import com.opencloud.system.service.SystemActionService;
import com.opencloud.system.service.SystemMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author liuyadu
 */
@Api(tags = "系统菜单信息管理")
@RestController
public class SystemMenuController implements ISystemMenuServiceClient {
    @Autowired
    private SystemMenuService systemMenuService;

    @Autowired
    private SystemActionService systemActionService;

    @Autowired
    private XRestTemplate xRestTemplate;

    @ApiOperation(value = "获取菜单列表", notes = "获取菜单列表")
    @GetMapping("/menu")
    @Override
    public Result<Page<SystemMenu>> getPage(@RequestParam Map<String, Object> map) {
        return Result.ok().data(systemMenuService.findPage(new PageParams(map)));
    }

    @ApiOperation(value = "获取菜单列表", notes = "获取菜单列表")
    @GetMapping("/menu/list")
    @Override
    public Result<List<SystemMenu>> getList() {
        return Result.ok().data(systemMenuService.findList());
    }

    @ApiOperation(value = "获取菜单下所有操作", notes = "获取菜单下所有操作")
    @GetMapping("/menu/action")
    @Override
    public Result<List<SystemAction>> getMenuAction(@RequestParam("menuId") Long menuId) {
        return Result.ok().data(systemActionService.findListByMenuId(menuId));
    }

    @ApiOperation(value = "获取菜单信息详情", notes = "获取菜单信息详情")
    @GetMapping("/menu/info")
    @Override
    public Result<SystemMenu> get(@RequestParam("menuId") Long menuId) {
        return Result.ok().data(systemMenuService.getById(menuId));
    }

    @ApiOperation(value = "添加/修改菜单信息", notes = "添加/修改菜单信息")
    @PostMapping("/menu/save")
    @Override
    public Result<Long> save(@RequestBody SystemMenu systemMenu) {
        if (systemMenu.getMenuId() == null || systemMenu.getMenuId() == 0) {
            // 确保新增的时候的id是空， 不然如果传过来是0， 就会导致新增的menuId是0
            systemMenu.setMenuId(null);
            systemMenuService.addAndServiceId(systemMenu);
        } else {
            systemMenuService.update(systemMenu);
        }
        return Result.ok();
    }

    @ApiOperation(value = "移除菜单信息", notes = "移除菜单信息")
    @PostMapping("/menu/remove")
    @Override
    public Result<Boolean> remove(@RequestParam("menuId") Long menuId) {
        systemMenuService.remove(menuId);
        xRestTemplate.refreshGateway();
        return Result.ok();
    }

    @ApiOperation(value = "获取app下的菜单列表", notes = "获取app下的菜单列表")
    @GetMapping("/menu/app/list")
    @Override
    public Result<List<SystemMenu>> getAppMenuList(@RequestParam("appId") Long appId) {
        return Result.ok().data(systemMenuService.findAuthorityMenuByApp(appId));
    }
}
